package com.study.remoteServerIo.controller;

import com.study.remoteServerIo.utils.SftpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author wei.zhao
 * @description:
 * @date 2022年07月13日 14:31
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    SftpUtil sftpUtil;

    /**
     * @param path     文件夹路径
     * @param fileName 文件名称
     * @param content  内容
     * @author wei.zhao
     * @date 2022/7/13 17:44
     * @description 上传文件
     */
    @PostMapping("/upload")
    public void upload(@RequestParam("path") String path, @RequestParam("fileName") String fileName, @RequestParam("content") String content) throws Exception {
        sftpUtil.uploadFile(path, fileName, content);
    }

    /**
     * @param path 文件夹路径
     * @return java.util.List<java.lang.String>
     * @author wei.zhao
     * @date 2022/7/13 17:45
     * @description 获取文件夹下的所有文件名称
     */
    @PostMapping("/getFileList")
    public List<String> getFileList(@RequestParam("path") String path) throws Exception {
        return sftpUtil.getFileList(path);
    }

    /*
     * @description 下载文件
     * @author wei.zhao
     * @date 2022/8/7 15:55
     * @param path
     * @param response
     */
    @GetMapping("/download")
    public void download(@RequestParam("path") String path, HttpServletResponse response) throws Exception {
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=testDownload.pdf");
        sftpUtil.downloadFile(path, response.getOutputStream());
    }

}
